package com.suket.jnn;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LayerFactory {

  static Logger logger = Logger.getLogger(LayerFactory.class);

  public static Layer createLayer( Layer previousLayer , String name ,
      int totalNodes , Random random ) {
    Layer layer = null;
    if ( ( name == null ) || ( name.equals("") ) ) {
      logger.log(Level.WARN, "Failed to create layer , found null name");
      return layer;
    }
    if ( totalNodes < 1 ) {
      logger.log(Level.WARN, "Failed to create layer , found zero total nodes");
      return layer;
    }
    int totalWeights = getTotalWeights(previousLayer);
    logger.log(Level.DEBUG, "Calculated total weights = " + totalWeights
        + " , based on previous layer");
    List listNodes = new ArrayList();
    for ( int i = 0 ; i < totalNodes ; i++ ) {
      listNodes.add(NodeFactory.createNode(totalWeights, random));
    }
    layer = new Layer();
    layer.setName(name);
    layer.setNodes(listNodes);
    logger.log(Level.DEBUG, "Created a new layer with name = " + name
        + " , total = " + LayerSupport.getTotalNodes(layer) + " nodes");
    return layer;
  }

  public static int getTotalWeights( Layer previousLayer ) {
    int totalWeights = 0;
    if ( previousLayer == null ) {
      logger.log(Level.WARN, "Failed to get total weights "
          + ", found null previous layer");
      return totalWeights;
    }
    // total weight ~ total previous layer nodes
    totalWeights = LayerSupport.getTotalNodes(previousLayer);
    return totalWeights;
  }

}
